package com.liu.efk.zdcq.netty;


import com.liu.common.utils.JacksonUtil;
import com.liu.efk.zdcq.pojo.ClientManager;
import com.liu.efk.zdcq.pojo.Message;


import com.liu.efk.zdcq.pojo.Task;
import com.liu.efk.zdcq.socket.WebSocket;
import com.liu.efk.zdcq.util.DateTimeUtil;
import com.liu.efk.zdcq.util.ImageUtils;
import com.liu.efk.zdcq.util.MessageUtil;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
@ChannelHandler.Sharable
@Slf4j
public class ServerHandler extends SimpleChannelInboundHandler<String> {


     @Autowired
    WebSocket webSocket;

    protected void messageReceived(ChannelHandlerContext channelHandlerContext, String s) throws Exception {
        log.info("接收到的消息为："+s+">###");
       Message message = MessageUtil.messageDecode(s);
//      log.info(""+message);

        webSocket.sendMessageTo( JacksonUtil.toJSon(new Task("1234","1",JacksonUtil.toJSon(message))),"1234");
        String msg="";
        ClientManager.bind(message.getObjectAddress(),channelHandlerContext.channel());

        if (message.getCommandStyle().equalsIgnoreCase("1")){
            switch (Integer.parseInt(message.getDataID())){
                case 1:// 登录信息
                    msg ="###<TaskID="+message.getTaskID()+";TaskContent{ResultFlg=0;Content<>}>###";
                    break;
                case 2://心跳
                    String time = DateTimeUtil.getTodayChar19();
                    msg ="###<TaskID=" + message.getTaskID() + ";TaskContent{ResultFlg=0;Content<DateTime=" + time + ">}>###";
                    break;
                case 3:// 上报图片信息
                    System.out.println("照片处理业务");
                    boolean flag = ImageUtils.base64DecodeImage(message.getImage(),"d:/image","d:/image/123/"+ System.currentTimeMillis() +".jpg",false);
                    System.out.println("照片结果："+flag);
                    msg ="###<TaskID="+message.getTaskID()+";TaskContent{ResultFlg=0;Content<>}>###";
                    break;
                case 4:// 设备运行信息
                    msg ="###<TaskID="+message.getTaskID()+";TaskContent{ResultFlg=0;Content<>}>###";
                    break;
                case 5:// 上报错误信息
                    msg ="###<TaskID="+message.getTaskID()+";TaskContent{ResultFlg=0;Content<>}>###";
                    break;
                case 6:// 上报自检日志
                    System.out.println("设备自检业务");
                    msg ="###<TaskID="+message.getTaskID()+";TaskContent{ResultFlg=0;Content<>}>###";
                    break;
                case 7:// 上报传感器数据
                    msg ="###<TaskID="+message.getTaskID()+";TaskContent{ResultFlg=0;Content<>}>###";
                    break;

            }
        }

//       if(message.getResultFlg()=="1"){
//           log.info("确认消息，下发参数",message.getResultFlg());
//           msg ="###<TaskID="+message.getTaskID()+";TaskContent{ResultFlg=0;Content<>}>###";
//       }





        log.info("发送信息为：" +msg);

        channelHandlerContext.writeAndFlush(msg);

    }

//    ###<TaskID=00188;TaskFlag=1;TaskContent{ObjectAddress=sx0047;CommandStyle=1;DataID=1;DataContent<Address=sx0047;SoftVersion=SV120190903;HardwareVersion=HV120190902>}>###



    /**
     * 新客户端接入
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {

        ClientManager.addClient(ctx.channel());
        System.out.println("channelActive");


    }

    /**
     * 客户端断开
     */
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        ClientManager.removeClient(ctx.channel());
        System.out.println("channelInactive");
    }
    /**
     * 异常
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
//        处理客户端异常关闭
         Channel incoming =ctx.channel();
         if(!incoming.isActive()){
             System.out.println("SimpleClient:" + incoming.remoteAddress()
                     + "异常");

             cause.printStackTrace();
             ctx.close();
         }

    }

}
